//
//  HYOriginalViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYOriginalViewController.h"
#import "CJOriginalTableViewCell.h"
#import "HYMoreShortModel.h"
#import "PKRequestManager.h"
#import "PKMusicPlayer.h"

@interface HYOriginalViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *originalTableView;
@property (nonatomic, strong) NSMutableArray *sourceArray;    //数据源

@property (nonatomic, strong) PKMusicPlayer *player;   //播放器
@property (nonatomic, assign) NSInteger index;  //记录上次播放的音乐坐标
@property (nonatomic, strong) NSTimer *timer;   //定时器

@end

@implementation HYOriginalViewController




//电影原声
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRandomClolor;
    _sourceArray = [NSMutableArray array];


    [self requestData];


    //定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerPlay) userInfo:nil repeats:YES];
    
}



- (void)requestData {
    
    //http://morguo.com/forum.php?mod=threadmusic&androidflag=1&appfrom=ios&iosversion=3.13&page=1
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=threadmusic&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        NSArray *array = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in array) {
            HYMoreShortModel *model = [[HYMoreShortModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }

        
        [self addPlayer];
        [self addTableView]; 
    } error:^(NSError *error) {
        
    }];
  
}



- (void)addPlayer {
    
    _player = [PKMusicPlayer sharedPlayer];
    NSMutableArray *array = [NSMutableArray array];
    for (HYMoreShortModel *CJmodel in _sourceArray) {
        [array addObject:CJmodel.musicurl];
    }
    _player.musicArray = array;
    
}



- (void)addTableView {
    
    _originalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    _originalTableView.dataSource = self;
    _originalTableView.delegate = self;
    [self.view addSubview:_originalTableView];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _sourceArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"CJOriginalTableViewCell";
    CJOriginalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CJOriginalTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (_player.index == indexPath.row && _player.isPlaying) {
        cell.imageButton.selected = YES;
    } else {
        cell.imageButton.selected = NO;
    }
    cell.model = _sourceArray[indexPath.row];
    cell.imageButton.tag = 2200 + indexPath.row;
    [cell.imageButton addTarget:self action:@selector(clickImageButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return cell;
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [CJOriginalTableViewCell cellOfContentLabelHeight:_sourceArray[indexPath.row]];
    
    return height + 300;
}


//点击播放音乐
- (void)clickImageButton:(UIButton *)button {
    
    
    button.selected = !button.selected;
    NSInteger num = button.tag - 2200;
    
    
    
    //点击播放或暂停
    if (button.selected) {
        [_player changeMusicWithIndex:num];
    } else {
        [_player pause];
    }

    
    //判断是不是上一首
    if (num != _index) {
        
        //改变上一首button的状态
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_index inSection:0];
        CJOriginalTableViewCell *cell = [_originalTableView cellForRowAtIndexPath:indexPath];
        cell.imageButton.selected = !cell.imageButton.selected;
    }
    //记录当前的标
    _index = _player.index;

}



- (void)timerPlay {
 
    if (_player.currentTime == _player.finishTime && _player.finishTime != 0) {
        [_player finishPlay];
    }
    
    if (_index != _player.index && _player.isPlaying) {
        //改变上一首button的状态
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_index inSection:0];
        CJOriginalTableViewCell *cell = [_originalTableView cellForRowAtIndexPath:indexPath];
        cell.imageButton.selected = !cell.imageButton.selected;
        
        //改变正在播放的button的状态
        NSIndexPath *indexPathn = [NSIndexPath indexPathForRow:_player.index inSection:0];
        CJOriginalTableViewCell *celln = [_originalTableView cellForRowAtIndexPath:indexPathn];
        celln.imageButton.selected = !celln.imageButton.selected;
   
        _index = _player.index;
    }
    
    

}



- (void)viewWillDisappear:(BOOL)animated {
    
    [_timer invalidate];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
